package com.controller;

import com.dto.Result;
import com.entity.ElderInfo;
import com.service.ElderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/elder/info")
public class ElderInfoController {

    @Autowired
    private ElderInfoService elderInfoService;

    @PostMapping
    public ElderInfo create(@RequestBody ElderInfo elderInfo) {
        return elderInfoService.save(elderInfo);
    }

    @PostMapping("/idCard")
    public ElderInfo saveByIdCard(@RequestBody ElderInfo elderInfo) {
        return elderInfoService.saveByIdCard(elderInfo.getIdCard(), elderInfo);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Integer id) {
        elderInfoService.deleteById(id);
    }

    @PutMapping("/{id}")
    public ElderInfo update(@PathVariable("id") Integer id, @RequestBody ElderInfo elderInfo) {
        elderInfo.setId(id);
        return elderInfoService.update(elderInfo);
    }

    @GetMapping("/idCard/{idCard}")
    public ElderInfo detail(@PathVariable("idCard") String idCard) {
        return elderInfoService.findByIdCard(idCard);
    }

    @GetMapping("/list")
    public List<ElderInfo> list() {
        return elderInfoService.findAll();
    }


    @GetMapping("/page")
    public Page<ElderInfo> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize);
        return elderInfoService.findAll(pageRequest);
    }


    @GetMapping("/search")
    public List<ElderInfo>  search(@RequestParam String name) {
        return elderInfoService.findByName(name);
    }
}